﻿@inject IStringLocalizer<Tabs> Localizer

<p>
    <Button Icon="fa-solid fa-circle-plus" OnClickWithoutRender="@(() => AddTab(TabSet))" Text="@Localizer["AddButtonText"]">
    </Button>
    <Button Color="Color.Danger" Icon="fa-solid fa-circle-minus" IsDisabled="@RemoveEndable" OnClickWithoutRender="@(() => RemoveTab(TabSet))" Text="@Localizer["RmoveButtonText"]">
    </Button>
    <Button Icon="fa-solid fa-font-awesome" OnClickWithoutRender="@(() => Active(TabSet))" Text="@Localizer["ActiveButtonText"]">
    </Button>
</p>
<Tab IsBorderCard="true" @ref="TabSet">
    <TabItem Text="@Localizer["TabItem1Text"]">
        <div>@Localizer["TabItem1Content"]</div>
    </TabItem>
    <TabItem Text="@Localizer["TabItem2Text"]">
        <div>@Localizer["TabItem2Content"]</div>
    </TabItem>
    <TabItem Text="@Localizer["TabItem3Text"]">
        <div>@Localizer["TabItem3Content"]</div>
    </TabItem>
    <TabItem Text="@Localizer["TabItem4Text"]">
        <div>@Localizer["TabItem4Content"]</div>
    </TabItem>
    <TabItem Text="@Localizer["TabItem7Text"]">
        <div>@Localizer["TabItem7Content"]</div>
    </TabItem>
</Tab>

@code {
    [NotNull]
    private Tab? TabSet { get; set; }

    private Task AddTab(Tab tabset)
    {
        var text = $"Tab {tabset.Items.Count() + 1}";
        tabset.AddTab(new Dictionary<string, object?>
        {
            [nameof(TabItem.Text)] = text,
            [nameof(TabItem.IsActive)] = true,
            [nameof(TabItem.ChildContent)] = new RenderFragment(builder =>
            {
                var index = 0;
                builder.OpenElement(index++, "div");
                builder.AddContent(index++, Localizer["BackAddTabText", text]);
                builder.CloseElement();
            })
        });
        return Task.CompletedTask;
    }

    private static Task Active(Tab tabset)
    {
        tabset.ActiveTab(0);
        return Task.CompletedTask;
    }

    private bool RemoveEndable => (TabSet?.Items.Count() ?? 4) < 4;

    private static async Task RemoveTab(Tab tabset)
    {
        if (tabset.Items.Count() > 4)
        {
            var item = tabset.Items.Last();
            await tabset.RemoveTab(item);
        }
    }
}
